<template>
  <div class="update-name">
    <van-nav-bar
    title="修改昵称"
    left-text="取消"
    right-text="完成"
    @click-left="$emit('close')"
    @click-right="onConfirm"
    >
    </van-nav-bar>
    <div class="name-field-wrap">
      <van-field
        v-model="localname"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入姓名"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateName',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localname: this.value
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true
      })
      try {
        await updateUserProfile({
          name: this.localname
        })
        this.$emit('close')
        this.$emit('input', this.localname)
      } catch (error) {
        if (error && error.response && error.response.status === 409) {
          this.$toast.fail('昵称已经存在')
        }
      }
      this.$toast.success('保存成功')
    }
  }
}
</script>

<style lang="less" scoped>
.name-field-wrap{
  padding: 10px;
}
</style>
